package com.qingfeng.core.dao;

import com.qingfeng.core.config.database.BaseMapper;
import com.qingfeng.core.entity.Permission;
import com.qingfeng.core.mapper.PermissionMapper;
import com.qingfeng.core.utils.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description: 权限dao
 * @author:LWY
 * @date: 2017/10/16 17:16
 * @modified:
 */
@Repository
public class PermissionDao extends BaseDao<Permission> {
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    protected BaseMapper<Permission> getBaseMapper() {
        return permissionMapper;
    }

    /**
     * 查询角色所有的权限
     *
     * @param role_id 角色id
     * @return
     */
    public Set<Permission> byRole(String role_id) {
        return permissionMapper.byRole(role_id);
    }

    /**
     * 根据权限菜单类型和用户获取权限菜单
     *
     * @param permission_type 类型（0：菜单，1：按钮）
     * @param userinfo_id     用户id
     * @return
     */
    public List<Permission> byTypeAndUserinfo(Integer permission_type, String userinfo_id) {
        if (permission_type < 0) {
            permission_type = null;
        }
        return permissionMapper.byTypeAndUserinfo(permission_type, userinfo_id);
    }


    /**
     * 根据父菜单和用户获取权限按钮
     *
     * @param p_id        父菜单id
     * @param userinfo_id 用户id
     * @return
     */
    public List<Permission> getButtonByPidAndUserinfo(String p_id, String userinfo_id) {
        return permissionMapper.getButtonByPidAndUserinfo(p_id, userinfo_id);
    }


    /**
     * 保存
     *
     * @param p_id             父节点id
     * @param permission_name  权限名称
     * @param icon_url         icon_url
     * @param permission_type  权限类型
     * @param url              url
     * @param permission_order 顺序
     * @return
     */
    public Permission save(String id, String p_id, String permission_name, String icon_url, String permission_type, String url, String method, String permission_order, String create_id) {
        Permission permission = new Permission();
        if (StringUtils.isNotEmpty(permission_name)) {
            permission.setPermission_name(permission_name);
        }
        if (StringUtils.isNotEmpty(permission_type)) {
            permission.setPermission_type(Integer.valueOf(permission_type));
        }
        if (StringUtils.isNotEmpty(url)) {
            permission.setUrl(url);
        }
        if (StringUtils.isNotEmpty(method)) {
            permission.setMethod(method);
        }
        if (StringUtils.isNotEmpty(permission_order)) {
            permission.setPermission_order(Integer.valueOf(permission_order));
        }
        if (StringUtils.isNotEmpty(icon_url)) {
            permission.setIcon_url(icon_url);
        }
        if (StringUtils.isEmpty(id)) {
            id = UuidUtil.generatorUUID();
            permission.setId(id);
            permission.setCreate_time(new Date());
            permission.setCreate_id(create_id);
            if (StringUtils.isNotEmpty(p_id)) {
                permission.setP_id(p_id);
                permission.setPath(byId(p_id).getPath() + "/" + id);
            } else {
                permission.setPath("/" + id);
            }
            permissionMapper.insertSelective(permission);
        } else {
            permission.setId(id);
            permissionMapper.updateByPrimaryKeySelective(permission);
        }
        return permissionMapper.selectByPrimaryKey(id);
    }


    /**
     * 删除权限
     *
     * @param ids id集合
     */
    @Override
    public int del(String[] ids) {
        int i = 0;
        for (String id : ids) {
            //先删除权限角色关联，后删除权限，注意顺序不能乱
            permissionMapper.delRole(id);
            i += permissionMapper.del(id);
        }
        return i;
    }

    /**
     * 获取所有权限并且标记某角色有哪些权限
     *
     * @param role_id
     * @return
     */
    public List<Map> forRole(String role_id) {
        return permissionMapper.forRole(role_id);
    }
}
